'use strict';

angular.module('app')
	.controller('userController', [ '$rootScope', '$scope', '$http', '$state',
	                                function($rootScope, $scope, $http, $state) {
		$scope.title = '用户管理';
        $scope.param = { };
        $scope.loading = false;

        console.log('user');
        console.log($scope.param);

		$scope.search = function () {
	        $scope.loading = true;
			$.ajax({
				url : $scope.app.project_name+'/user/read/list',
				data: $scope.param
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result.data;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
		
		$scope.searchRe = function () {
	        $scope.loading = true;
	        $scope.param.pageNum = 1;
			$.ajax({
				url : $scope.app.project_name+'/user/read/list',
				data: $scope.param
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result.data;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
		
		$scope.search();
		
		$scope.clearSearch = function() {
			$scope.param.keyword= null;
			$scope.search();
		}
		
		$scope.disableItem = function(id, enable) {
			$.ajax({
				url : $scope.app.project_name + '/user/update/disable',
				data: {'id': id, 'enable' : enable}
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.param.keyword= null;
					$scope.search();
				} else {
					$scope.msg = result.msg;
				}
			});
		}

		$scope.resetPassword = function(id) {
			$scope.alertMessage({
                    text:"确定做此操作吗？", //内容
                    btnCancel:true, //取消按钮
                    btnConfirm:true //确认按钮
                }).then(function () {
				$.ajax({
					url : $scope.app.project_name + '/user/update/resetPassword',
					data: {'id': id}
				}).then(function(result) {
			        $scope.loading = false;
					if (result.httpCode == 200) {
						$scope.param.keyword= null;
						if ("0" === result.data) {
							$scope.alertModal({"content":"重置成功"});
						} else if ("1" === result.data) {
							$scope.alertModal({"content":"没有找到用户"});
						} else if ("2" === result.data) {
							$scope.alertModal({"content":"重置失败"});
						} else {
							$scope.alertModal({"content":"用户的email为空"});
						}
						$scope.search();
					} else {
						$scope.msg = result.msg;
					}
				});
			})
		}
		
		
		// 翻页
        $scope.pagination = function (pageNo) {
            $scope.param.pageNum=pageNo;
            $scope.search();
        };
} ]);